package cn.icare.cms.controller.restful;

import cn.icare.cms.ItemCatService;
import cn.icare.cms.model.ro.AddItemCatRO;
import cn.icare.cms.model.ro.EditItemCatRO;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/itemcat")
@Api(tags = "REST商品分类")
public class ItemCatController extends BaseController {
    @Resource
    private ItemCatService itemCatService;
    private final String FALL_STRING = "请求失败,服务繁忙!";

    /**
     * URL:http://{ip}:{port}/rest/itemcat
     * method:POST
     *
     * @param ro
     * @return
     */
    @PostMapping()
    @HystrixCommand(fallbackMethod = "createOneFall")
    @ApiOperation(value = "新增单个商品分类", notes = "t_itemcat表新增单个商品分类")
    public ResponseEntity<RespMsgVO> createOne(@RequestBody AddItemCatRO ro) {
        //判断是否重复
        boolean isExists = this.itemCatService.isExists(0L,ro.getParentId(), ro.getName());
        if (isExists) {
            return super.getRespFail("商品分类名称已经存在，请不要重复添加");
        }
        Long id = this.itemCatService.createOne(ro);
        if (id >= 0) {
            return super.getRespOk(id);
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/itemcat
     * method:PUT
     *
     * @param ro
     * @return
     */
    @PutMapping()
    @HystrixCommand(fallbackMethod = "updateOneFall")
    @ApiOperation(value = "更新单个商品分类", notes = "t_itemcat表更新单个商品分类")
    public ResponseEntity<RespMsgVO> updateOne(@RequestBody EditItemCatRO ro) {

        //判断是否重复
        boolean isExists = this.itemCatService.isExists(ro.getId(),ro.getParentId(), ro.getName());
        if (isExists) {
            return super.getRespFail("商品分类名称已经存在，请更换");
        }
        if (this.itemCatService.updateOne(ro)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/itemcat/{id}
     * method:DELETE
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @HystrixCommand(fallbackMethod = "deleteOneFall")
    @ApiOperation(value = "删除单个商品分类", notes = "usergroup表删除符合主键的单个商品分类")
    public ResponseEntity<RespMsgVO> deleteOne(@PathVariable("id") Long id) {
        if (this.itemCatService.deleteOne(id)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/itemcat?ids={ids}
     * method:DELETE
     *
     * @param ids
     * @return
     */
    @DeleteMapping()
    @HystrixCommand(fallbackMethod = "deleteBatchFall")
    @ApiOperation(value = "删除多个商品分类", notes = "usergroup表删除符合主键的多个商品分类")
    public ResponseEntity<RespMsgVO> deleteBatch(@RequestParam("ids") List<Long> ids) {
        if (this.itemCatService.deleteBatch(ids)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/itemcat/{id}
     * method:GET
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getOneFall")
    @ApiOperation(value = "查询单个商品分类", notes = "usergroup表返回符合主键的单个商品分类")
    public ResponseEntity<RespMsgVO> getOne(@PathVariable("id") Long id) {
        return super.getRespOk(this.itemCatService.getOne(id));
    }

    /**
     * URL:http://{ip}:{port}/rest/itemcat?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping()
    @HystrixCommand(fallbackMethod = "getListFall")
    @ApiOperation(value = "查询多个商品分类", notes = "user表返回符合条件的多个商品分类")
    public ResponseEntity<RespMsgVO> getList(@RequestParam(name = "name", required = false) String name,
                                             @RequestParam(name = "pageNum", required = false, defaultValue = "${cms.defaultValue.pageNum}") Integer pageNum,
                                             @RequestParam(name = "pageSize", required = false, defaultValue = "${cms.defaultValue.pageSize}") Integer pageSize,
                                             @RequestParam(name = "parentId", required = false, defaultValue = "-1") Long parentId
    ) {
        return super.getRespOk(this.itemCatService.getList(parentId, name, pageNum, pageSize));
    }


    //#region fallback methods
    private ResponseEntity<RespMsgVO> createOneFall(AddItemCatRO ro) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> updateOneFall(EditItemCatRO ro) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteOneFall(Long id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteBatchFall(List<Long> ids) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getOneFall(Long id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getListFall(String name, Integer pageNum, Integer pageSize, Long parentId) {
        return super.getRespFail(this.FALL_STRING);
    }
    //#endregion

}
